<?php
/**
 * 集团公文控制器
 * 作者：莫凯
 * 日期：2014/1/10
 */
class DocumentAction extends MessageAction {
	/*添加公文流程*/
	public function addDocument(){
		$p=M('Process');
		$document=M('Document');
        $number=$_POST['process_num'];
        $number=intval(substr($number, 0,10));
        $result=$p->where("process_num='".$number."' and process_state=1")->find();//已经申请
        if($result){
            echo '该申请已提交！';
        }
        else{
        	$result_second=$p->where("process_num='".$number."' and process_state=0")->find();//如果已经保存了
            if($result_second){
                $dataProcess['process_state']=1;
                $data['document_state']=1;
                $p->where("process_num='".$number."'")->save($dataProcess);
                $field=$p->where("process_num='".$number."'")->field('process_id,process_begin_id,process_now_id,process_title')->find();
                $document->where("document_process_id='".$field['process_id']."'")->save($data);

                $msg_from_name=$this->getOneEmployeName($field['process_begin_id']);
                $msg_title=$field['process_title'];
                $msg_content=$msg_title.$msg_from_name.'向您提交了一条申请，请注意查收';
                if(I('msg_tip')){
                    //发送消息
                    $this->sendMsg($field['process_now_id'],$field['process_begin_id'],$msg_title,$msg_content);
                }

                if(I('msg_phone')){
                    //发送短信
                    $mobile=I('phone_num');
                    $content=$msg_content."【来自丽丰OA系统】";
                    $mobileids=$mobile.strval(time());
                    $this->sendSMS($mobile,$content,$mobileids);
                }
                
                echo '申请成功！';
            }
            else{

				$data=$_POST;
				/*流程数据*/
				$process["process_num"]=$number;
				$process['process_title']="公文流程单";
				$process['process_begin_id']=$data['document_author'];
				$process['process_now_id']=$data['document_proofreader'];
				$process['process_time']=time();
				$process['process_state']=$data['document_tag'];
				$process['process_type']='Document';
				$process['process_now_id_if_approval']=0;
				$process['process_parent_id']=0;
				$m=M("process");
				$id=$m->add($process);
				dump($id);
				if(false==$id){
					echo 0;
					exit;
				}else{
					$data['document_process_id']=$id;
					$m=M("Document");
					$id1=$m->add($data);
					if(false==$id1){
						echo 0;
						exit;
					}else{
						$msg_from_name=$this->getOneEmployeName($data['document_author']);
		                $msg_content=$msg_from_name.'向您提交了一条集团公文签批单，请注意查收';
		                if($data['msg_tip']){
		                    //发送消息
		                    $msg_title="集团公文签批单";
		                    $this->sendMsg($data['document_proofreader'],$data['document_author'],$msg_title,$msg_content);
		                }

		                if($data['msg_phone']){
		                    //发送短信
		                    $mobile=I('phone_num');
		                    $content=$msg_content."【来自丽丰OA系统】";
		                    $mobileids=$mobile.strval(time());
		                    $this->sendSMS($mobile,$content,$mobileids);
		                }

						echo '申请成功！';
						exit;
					}
				}
		}

	}
		
	}


	public function saveDocumentPost(){
		$p=M('Process');
        $number=$_POST['process_num'];
        $number=intval(substr($number, 0,10));
        $result=$p->where("process_num='".$number."'")->find();//已经申请
        if($result){
            echo '该申请已保存！';
        }
        else{
			$data=$_POST;
			/*流程数据*/
			$process["process_num"]=$number;
			$process['process_title']="公文流程单";
			$process['process_begin_id']=$data['document_author'];
			$process['process_now_id']=$data['document_proofreader'];
			$process['process_time']=time();
			$process['process_state']=$data['document_tag'];
			$process['process_type']='Document';
			$process['process_now_id_if_approval']=0;
			$process['process_parent_id']=0;
			$m=M("process");
			$id=$m->add($process);
			dump($id);
			if(false==$id){
				echo 0;
				exit;
			}else{
				$data['document_process_id']=$id;
				$m=M("Document");
				$id1=$m->add($data);
				if(false==$id1){
					echo 0;
					exit;
				}else{
					echo '申请成功！';
					exit;
				}
			}
		}
	        
	}


}